import { MenuItemDefinition } from '@amcax/base';
import queryString from 'query-string';
import { ProductTreeNode } from '../../types';

export const open: MenuItemDefinition<ProductTreeNode> = {
  id: 'open',
  children: '编辑',
  hidden: ({ props: { parent, type } }) =>
    !parent || // root node has no parent
    !['product', 'part'].includes(type),
  onClick: ({
    props: {
      collectionUUID,
      assembly: { docid, docver },
    },
  }) => {
    const url = `?${queryString.stringify({
      col: collectionUUID,
      doc: docid,
      ver: docver,
    })}`;

    if (window.__MICRO_APP_ENVIRONMENT__) {
      (window as any).microApp.getGlobalData().history.push(url);
    } else {
      window.open(url, '_blank');
    }
  },
};
